package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yuwj.chinaa.R;
import com.yuwj.chinaa.adapter.ComputeCodeEarningRecordAdapter;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.entity.EntityComputeCodeEarningRecord;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ActivityCollectUtil;
import com.yuwj.chinaa.util.ToastUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ComputeCodeEarningRecordActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_earning_record)
    RecyclerView rvEarningRecord;
    List<EntityComputeCodeEarningRecord> entityComputeCodeEarningRecordList;
    ComputeCodeEarningRecordAdapter computeCodeEarningRecordAdapter;
    IPresenter presenter;

    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, ComputeCodeEarningRecordActivity.class);
        return intent;
    }

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_computer_code_earning_record;
    }

    @Override
    public void initParam() {
        presenter = new PresenterImpl(this);
        entityComputeCodeEarningRecordList = new ArrayList<>();
        computeCodeEarningRecordAdapter = new ComputeCodeEarningRecordAdapter(context, entityComputeCodeEarningRecordList);
    }

    @Override
    public void initView() {
        initActionBar(toolbar, tvTitle, "算码收益记录");
        rvEarningRecord.setAdapter(computeCodeEarningRecordAdapter);
        rvEarningRecord.setLayoutManager(new LinearLayoutManager(context));

        computeCodeEarningRecordAdapter.setEmptyView(R.layout.content_compute_code_earning_record_empty, (ViewGroup) rvEarningRecord.getParent());
        View emptyView = computeCodeEarningRecordAdapter.getEmptyView();
        RelativeLayout rlToBuyMiner = (RelativeLayout) emptyView.findViewById(R.id.rl_to_buy_miner);
        rlToBuyMiner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityCollectUtil.getInstance().finishAll();
                startActivity(MainActivity.createIntent(context));
            }
        });
    }

    @Override
    public void initOperator() {
        JSONObject param = new JSONObject();
        presenter.queryMinerProfitList(param.toString());

    }

    @Override
    public void onQueryMinerProfitListSuccess(List<EntityComputeCodeEarningRecord> list) {
        super.onQueryMinerProfitListSuccess(list);
        entityComputeCodeEarningRecordList.clear();
        entityComputeCodeEarningRecordList.addAll(list);
        computeCodeEarningRecordAdapter.notifyDataSetChanged();
    }

    @Override
    public void onQueryMinerProfitListFail(Exception e) {
        super.onQueryMinerProfitListFail(e);
        ToastUtil.getInstance().shortLength(context, e.getMessage());
    }
}
